<script>
import {
  LocalGame,
  Follow,
  Followed,
  GameLive,
  // BullTalk,
  Trade,
  // BullLive,
  Exchange,
  NewGame,
  NewRecom,
  NewArticle,
  LearnOthers,
  // OpenBusiness,
  GetMedal,
  WinGame
} from './'
import BottomBar from './common/BottomBar'
import RemoteDAO from '@/tools/remoteDAO'

const momentMap = {
  'GM_LOCAL': LocalGame, // 本地比赛
  'JYB_OF_FOLLOWEES': Followed, // 订阅我的人
  'GM_NEW': NewGame, // 新比赛
  'GM_OF_FOLLOWER': Follow, // 我订阅的人
  'LH_NEW_ARTICLE': NewArticle, // 老何新发文推送
  'JXNG_NEW_ARTICLE': NewRecom, // 精选牛股新文章推送
  'SQ_JYB_DYNAMIC': Exchange, // 君元宝兑换商品详情
  'SQ_MEDAL_DYNAMIC': GetMedal, // 社区勋章动向
  'JY_DIRECTION': Trade, // 用户交易动向
  'USER_WON_INFO': WinGame, // 用户获胜信息
  'TS_INFO': LearnOthers, // 社区偷师信息推送
  'GM_LIVE': GameLive // 比赛实况
}

export default {
  name: 'smart-moment',
  functional: true,
  render (createElement, context) {
    const Props = JSON.parse(context.props.item.content)
    const {id: momentId, msgType: momentType} = context.props.item
    const {TIME, PUB_TIME, VIEW_NUM, UP_NUM, IS_LIKE, ID = momentId} = Props
    let botProps = {}
    if (momentType === 'GM_LOCAL' ||
      momentType === 'GM_LIVE' ||
      momentType === 'JYB_OF_FOLLOWEES' ||
      momentType === 'GM_OF_FOLLOWER') {
      botProps = {PUB_TIME, VIEW_NUM, UP_NUM, IS_LIKE, ID}
    } else {
      botProps = {TIME, VIEW_NUM, UP_NUM, IS_LIKE, ID}
    }
    function appropriateMomentComponent () {
      const { item } = context.props
      return momentMap[item.msgType]
    }
    return createElement(
      appropriateMomentComponent(), {
        props: {Props, momentId, momentType},
        nativeOn: { // 仅对于组件，用于监听原生事件，
          click: () => {
            RemoteDAO.browseMoment({targetId: momentId}).then(res => {
              console.warn(res)
            }, err => {
              throw err
            })
          }
        },
        scopedSlots: {
          default: function () {
            return createElement(BottomBar, {
              props: {botProps}
            })
          }
        }
      },
      context.children
    )
  },
  props: {
    item: Object
  }
}
</script>
